package cn.hhj.controller;

import cn.hhj.domain.User;
import cn.hhj.utils.FallbackExceptionUtil;
import cn.hhj.utils.LimitExceptionUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
// 动态刷新端口号的内容（端口号除外）
@RefreshScope
public class UserController {
//    @Value("${test.name}")
//    private String name;

//    @GetMapping("/getById/{id}")
//    // blockHandler：指定限流之后的托底方法
//    @SentinelResource(value = "getById",blockHandlerClass = LimitExceptionUtil.class,blockHandler = "handleException")
//    public User getById(@PathVariable("id") Long id){
//        return new User(id,"2022-08-10","阿里巴巴nb!");
//    }
    @GetMapping("/getById/{id}")
    @SentinelResource(value = "getById",fallback = "fallbackMethod",fallbackClass = FallbackExceptionUtil.class)
    public User getById(@PathVariable("id") Long id){
        // 因为模拟的是慢调用，所以直接让线程休眠
//        try {
//            // 用于让服务慢响应
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return new User(id,"2022-08-10","阿里巴巴nb!");
    }
    // 本类托底方法
//    public User handleException(@PathVariable("id") Long id, BlockException e){
//        e.printStackTrace();
//        return new User(-1L,"限流了","当前请求过大，请稍后重试！");
//    }

    // 本类熔断方法
//    public User fallbackMethod(@PathVariable("id") Long id, Throwable e){
//        e.printStackTrace();
//        return new User(-1L,"熔断了","当前请求服务不可用，请稍后重试！");
//    }
}
